/*
 * Copyright 2010 Mttang.com All right reserved. This software is the confidential and proprietary information of
 * Mttang.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Mttang.com.
 */
package com.mttang.sys.integration.dao.support;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.mttang.sys.dto.RoleDTO;
import com.mttang.sys.dto.RolePrivilegeDTO;
import com.mttang.sys.integration.dao.RoleDAO;

/**
 * 类RoleDAOImpl.java的实现描述：TODO 类实现描述
 * 
 * @author acc 2010-6-1 下午07:53:55
 */
public class RoleDAOImpl extends SqlMapClientDaoSupport implements RoleDAO {

    private Log logger = LogFactory.getLog(getClass());

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#delete(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void delete(RoleDTO dto) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---delete");
        }
        getSqlMapClientTemplate().delete("SYS.deleteRole", dto);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findById(java.lang.Integer)
     */
    @Override
    public RoleDTO findById(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findById");
        }
        return (RoleDTO) getSqlMapClientTemplate().queryForObject("SYS.selectRoleById", id);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findCount(java.lang.String)
     */
    @Override
    public Long findCount(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findCount");
        }
        return (Long) getSqlMapClientTemplate().queryForObject("SYS.countRole", param);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findUser(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<RoleDTO> findRole(Map<String, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findUser");
        }
        return getSqlMapClientTemplate().queryForList("SYS.selectRole", param);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#insert(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void insert(RoleDTO dto) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---insert");
        }
        getSqlMapClientTemplate().insert("SYS.insertRole", dto);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#update(com.mttang.sys.dto.RoleDTO)
     */
    @Override
    public void update(RoleDTO dto) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---update");
        }
        getSqlMapClientTemplate().update("SYS.updateRole", dto);
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findAll()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, String>> findAll() {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findAll()");
        }
        return getSqlMapClientTemplate().queryForList("SYS.selectRoleAll");
    }

    /*
     * (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findRoelPrv(java.lang.Integer)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<RolePrivilegeDTO> findRoelPrv(Integer id) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findRoelPrv()");
        }
        return getSqlMapClientTemplate().queryForList("SYS.rolePrivilege", id);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findPrv(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<RolePrivilegeDTO> findPrv(Map<Object, Object> param) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findPrv()");
        }
        if((String.valueOf(param.get("roleId")).split(",").length)>1){
        String[]  ss=String.valueOf(param.get("roleId")).substring(1, String.valueOf(param.get("roleId")).length()-1).split(",");
        param.put("ss", ss);
        }else{
            String[]  ss=String.valueOf(param.get("roleId")).split(",");
            param.put("ss", ss);
        }
        return getSqlMapClientTemplate().queryForList("SYS.rolePrv", param);
    }

    /* (non-Javadoc)
     * @see com.mttang.sys.integration.dao.RoleDAO#findById(java.lang.String)
     */
    @Override
    public RoleDTO findByName(String roleName) {
        // TODO Auto-generated method stub
        if (logger.isInfoEnabled()) {
            logger.info("---RoleDAOImpl---findById");
        }
        return (RoleDTO) getSqlMapClientTemplate().queryForObject("SYS.selectRoleByName", roleName);

    }

}
